home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 2 of 3.iso / chapter5 / drawex.c < prev    next >
C/C++ Source or Header  |  1996-03-06  |  7KB  |  246 lines

  1.  
  2. #include <windows.h>  
  3. #include "drawex.h"  
  4. #include "commctrl.h"
  5.  
  6.  
  7. #if defined (WIN32)
  8.     #define IS_WIN32 TRUE
  9. #else
  10.     #define IS_WIN32 FALSE
  11. #endif
  12.  
  13. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  14. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  15. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  16.  
  17. HINSTANCE hInst;   // current instance
  18.  
  19. LPCTSTR lpszAppName = "MyApp";
  20. LPCTSTR lpszTitle   = "ImageList_DrawEx()"; 
  21.  
  22.  
  23. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  24.  
  25.  
  26. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  27.                       LPTSTR lpCmdLine, int nCmdShow)
  28. {
  29.    MSG      msg;
  30.    HWND     hWnd; 
  31.    WNDCLASS wc;
  32.  
  33.    wc.style         = CS_HREDRAW | CS_VREDRAW;
  34.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  35.    wc.cbClsExtra    = 0;                      
  36.    wc.cbWndExtra    = 0;                      
  37.    wc.hInstance     = hInstance;              
  38.    wc.hIcon         = LoadIcon (hInstance, lpszAppName); 
  39.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  40.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  41.    wc.lpszMenuName  = lpszAppName;              
  42.    wc.lpszClassName = lpszAppName;              
  43.  
  44.    if ( IS_WIN95 )
  45.    {
  46.       if ( !RegisterWin95( &wc ) )
  47.          return( FALSE );
  48.    }
  49.    else if ( !RegisterClass( &wc ) )
  50.       return( FALSE );
  51.  
  52.    hInst = hInstance; 
  53.  
  54.    hWnd = CreateWindow( lpszAppName, 
  55.                         lpszTitle,    
  56.                         WS_OVERLAPPEDWINDOW, 
  57.                         CW_USEDEFAULT, 0, 
  58.                         CW_USEDEFAULT, 0,  
  59.                         NULL,              
  60.                         NULL,              
  61.                         hInstance,         
  62.                         NULL               
  63.                       );
  64.  
  65.    if ( !hWnd ) 
  66.       return( FALSE );
  67.  
  68.    ShowWindow( hWnd, nCmdShow ); 
  69.    UpdateWindow( hWnd );         
  70.  
  71.    while( GetMessage( &msg, NULL, 0, 0) )   
  72.    {
  73.       TranslateMessage( &msg ); 
  74.       DispatchMessage( &msg );  
  75.    }
  76.  
  77.    return( msg.wParam ); 
  78. }
  79.  
  80.  
  81. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  82. {
  83.    WNDCLASSEX wcex;
  84.  
  85.    wcex.style         = lpwc->style;
  86.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  87.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  88.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  89.    wcex.hInstance     = lpwc->hInstance;
  90.    wcex.hIcon         = lpwc->hIcon;
  91.    wcex.hCursor       = lpwc->hCursor;
  92.    wcex.hbrBackground = lpwc->hbrBackground;
  93.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  94.    wcex.lpszClassName = lpwc->lpszClassName;
  95.  
  96.    // Added elements for Windows 95.
  97.    //...............................
  98.    wcex.cbSize = sizeof(WNDCLASSEX);
  99.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  100.                             IMAGE_ICON, 16, 16,
  101.                             LR_DEFAULTCOLOR );
  102.             
  103.    return RegisterClassEx( &wcex );
  104. }
  105.  
  106. HIMAGELIST hList = NULL;
  107.  
  108. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  109. {
  110. static int nSel  = -1;
  111.  
  112.    switch( uMsg )
  113.    {
  114.       case WM_CREATE :
  115.               InitCommonControls();
  116.  
  117.               // Create the image list.
  118.               //.......................
  119.               hList = ImageList_Create( 32, 32, ILC_COLOR | ILC_MASK, 1, 1 );
  120.               if ( hList )
  121.               {
  122.                  HBITMAP hBmp;
  123.                  
  124.                  // Add an image and build a mask.
  125.                  //...............................
  126.                  hBmp = LoadBitmap( hInst, "BMP2" );
  127.                  ImageList_AddMasked( hList, hBmp, RGB( 0, 255, 0 ) );
  128.                  DeleteObject( hBmp );
  129.               }
  130.               break;
  131.  
  132.       case WM_PAINT :
  133.               {
  134.                  PAINTSTRUCT ps;
  135.  
  136.                  // Paint the two images on the client area.
  137.                  //.........................................
  138.                  BeginPaint( hWnd, &ps );
  139.                  ImageList_DrawEx( hList, 0, ps.hdc, 10, 10, 0, 0, 
  140.                                    CLR_DEFAULT, RGB( 255, 0, 0 ),
  141.                                    ILD_NORMAL | (nSel == 0 ? ILD_BLEND25 : 0) );
  142.                  EndPaint( hWnd, &ps );
  143.               }
  144.               break;
  145.  
  146.       case WM_COMMAND :
  147.               switch( LOWORD( wParam ) )
  148.               {
  149.                  case IDM_SELECT :
  150.                         nSel = (nSel == -1 ? 0 : -1);
  151.                         InvalidateRect( hWnd, NULL, TRUE );
  152.                         break;
  153.  
  154.                  case IDM_SETBKGRND :
  155.                         DialogBox( hInst, "RGBDialog", hWnd, (DLGPROC)RGBDialog ); 
  156.                         InvalidateRect( hWnd, NULL, TRUE );
  157.                         break;
  158.  
  159.                  case IDM_ABOUT :
  160.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  161.                         break;
  162.  
  163.                  case IDM_EXIT :
  164.                         DestroyWindow( hWnd );
  165.                         break;
  166.               }
  167.               break;
  168.       
  169.       case WM_DESTROY :
  170.               if ( hList )
  171.                  ImageList_Destroy( hList );
  172.  
  173.               PostQuitMessage(0);
  174.               break;
  175.  
  176.       default :
  177.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  178.    }
  179.  
  180.    return( 0L );
  181. }
  182.  
  183.  
  184. LRESULT CALLBACK RGBDialog( HWND hDlg,           
  185.                             UINT message,        
  186.                             WPARAM wParam,       
  187.                             LPARAM lParam)
  188. {
  189.    switch (message) 
  190.    {
  191.        case WM_INITDIALOG: 
  192.                {
  193.                   COLORREF color = ImageList_GetBkColor( hList );
  194.  
  195.                   SetDlgItemInt( hDlg, IDC_RED,   GetRValue( color ), FALSE );
  196.                   SetDlgItemInt( hDlg, IDC_GREEN, GetGValue( color ), FALSE );
  197.                   SetDlgItemInt( hDlg, IDC_BLUE,  GetBValue( color ), FALSE );
  198.                }    
  199.                return (TRUE);
  200.  
  201.        case WM_COMMAND:                              
  202.                if ( LOWORD(wParam) == IDOK )
  203.                {
  204.                   ImageList_SetBkColor( hList, RGB( 
  205.                                                GetDlgItemInt( hDlg, IDC_RED, NULL, FALSE ),
  206.                                                GetDlgItemInt( hDlg, IDC_GREEN, NULL, FALSE ),
  207.                                                GetDlgItemInt( hDlg, IDC_BLUE, NULL, FALSE ) ) );
  208.  
  209.                   EndDialog( hDlg, IDOK );        
  210.                   return (TRUE);
  211.                }
  212.                else if ( LOWORD(wParam) == IDCANCEL)    
  213.                {
  214.                   EndDialog( hDlg, IDCANCEL );        
  215.                   return (TRUE);
  216.                }
  217.                break;
  218.    }
  219.  
  220.    return (FALSE); 
  221. }
  222.  
  223.  
  224. LRESULT CALLBACK About( HWND hDlg,           
  225.                         UINT message,        
  226.                         WPARAM wParam,       
  227.                         LPARAM lParam)
  228. {
  229.    switch (message) 
  230.    {
  231.        case WM_INITDIALOG: 
  232.                return (TRUE);
  233.  
  234.        case WM_COMMAND:                              
  235.                if (   LOWORD(wParam) == IDOK         
  236.                    || LOWORD(wParam) == IDCANCEL)    
  237.                {
  238.                        EndDialog(hDlg, TRUE);        
  239.                        return (TRUE);
  240.                }
  241.                break;
  242.    }
  243.  
  244.    return (FALSE); 
  245. }
  246.